package com.woniu.controller;

import com.woniu.entity.ProductInput;
import com.woniu.service.ProductInputService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/productInput")
public class ProductInputController {
    @Resource
    private ProductInputService service;

    @GetMapping
    public ResponseUtil findAll(){ return service.findAll(); }

    @GetMapping("/{inputId}")
    public ResponseUtil findByInputId(@PathVariable Integer inputId){
        return service.findByInputId(inputId);
    }

    @PostMapping
    public ResponseUtil add(@RequestBody ProductInput productInput){ return service.add(productInput); }

    @DeleteMapping("/{inputId}")
    public ResponseUtil delete(@PathVariable Integer inputId){ return service.delete(inputId); }

    @PutMapping
    public ResponseUtil update(@RequestBody ProductInput productInput){ return service.update(productInput); }

    // 新增扫码入库接口
    @PostMapping("/scan")
    public ResponseUtil scanStockIn(@RequestParam String requisitionId) {
        return service.scanStockIn(requisitionId);
    }

    @GetMapping("/mobile/{inputId}")
    public ResponseUtil getInputForMobile(@PathVariable Integer inputId) {
        return service.getInputForMobile(inputId);
    }

    @PutMapping("/confirm")
    public ResponseUtil confirmStockIn(@RequestBody Map<String, Integer> params) {
        Integer inputId = params.get("inputId");
        ProductInput input = (ProductInput) service.findByInputId(inputId).getData();
        if (input == null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "记录不存在");
        }
        input.setCreateTime(new Date());
        input.setStatus(1); // 已入库
        return service.update(input);

    }
    @PutMapping("/confirm1")
    public ResponseUtil confirm(@RequestBody Map<String, Integer> params) {
        Integer inputId = params.get("inputId");
        ProductInput input = (ProductInput) service.findByInputId(inputId).getData();
        if (input == null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "记录不存在");
        }
        input.setStatus(0); // 撤销入库
        return service.update(input);
    }


    @GetMapping("/all")
    public ResponseUtil findAllVO(){ return service.findAllVO(); }
}